const db = wx.cloud.database()
let recordStartX = 0,
  currentOffsetX = 0,
  curIndex = 0; //按下初始值，当前滑块初始值，当前滑块下标
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataObj: "",
    list: [],
    offsetWidth: -250, //上面的盒子向左滑动时，下面的盒子要显示出来的宽度，负号是指相对于起始位置向左滑动为负值
    msg: '',
    cnt: 0,
    touchS: [0, 0],
    touchE: [0, 0],
    active: 'container'
  },
  active() {
    var container = document.getElementsByClassName("container")
    container.className += 'active'
  },
  // 触摸开始事件
  touchStart: function (e) {
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.data.touchS = [sx, sy]
    console.log(sx, sy)
  },
  // 触摸滑动事件
  touchMove: function (e) {
    let sx = e.touches[0].pageX;
    let sy = e.touches[0].pageY;
    console.log(sx, sy)
    this.data.touchE = [sx, sy]
  },
  // 触摸结束事件
  touchEnd: function (e) {
    let start = this.data.touchS
    let end = this.data.touchE
    let msg = ''
    if (start[0] < end[0] - 50) {
      if (this.data.active != 'container') return
      this.setData({
        active: 'container right'
      })
      let that = this
      setTimeout(res => {
        let cnt = this.data.cnt
        console.log(cnt)
        if (cnt == 0) cnt = this.data.list.length - 1
        else cnt--
        this.setData({
          active: 'container',
          cnt: cnt
        })
      }, 1500)
      msg = '向右滑'
    } else if (start[0] > end[0] + 50) {
      if (this.data.active != 'container') return
      this.setData({
        active: 'container left'
      })
      let that = this
      setTimeout(res => {
        let cnt = this.data.cnt
        if (cnt == this.data.list.length - 1) cnt = 0
        else cnt++
        that.setData({
          active: 'container',
          cnt: cnt
        })
      }, 1500)
      console.log('向左滑')
      // let cnt = this.data.cnt
      // if (cnt == this.data.list.length - 1) cnt = 0
      // else cnt++
      // this.setData({
      //   cnt: cnt
      // })
      msg = '向左滑'

    } else {
      console.log('上下滑动')
    }
    this.setData({
      msg: msg
    })
  },
  getImgUrl() {

  },
  getDataObj() {
    db.collection('test').get().then(res => {
      console.log(res)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    wx.cloud.callFunction({
      name: 'getMeituMeiyan'
    }).then(res => {
      console.log('openid==>',res.result.openid)
      // console.log('data==>',res.result.data.data)
      _this.setData({
        list:res.result.data.data
      })
    })
    // let _this = this
    // wx.request({
    //   url: "https://time-companion-cloud-7be17d583f6.ap-shanghai.kits.tcloudbase.com/cms/odzjegw5h3zjhfym/v1/open-api/projects/TimeCompanion/collections/MeituMeiyan/contents?limit=10&offset=0",
    //   header: {
    //     "content-type": "application/json",
    //   },
    //   success(res) {
    //     console.log("cms数据", res.data.result.data);
    //     _this.setData({
    //       list: res.data.result.data
    //     })
    //     console.log("list", _this.data.list);
    //   },
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})